//
//  XHUserNameViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/17.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHUserNameViewController.h"

#import "XHLabelTextFieldCell.h"

#import "Services.h"

@interface XHUserNameViewController ()

@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;

@end

@implementation XHUserNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0,0,44,44)];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button addTarget:self action:@selector(settingAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *settingItem=[[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.rightBarButtonItem=settingItem;

    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHLabelTextFieldCell" bundle:nil] forCellReuseIdentifier:XHLabelTextFieldCellIdentifier];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)settingAction:(UIButton*)button
{
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    XHLabelTextFieldCell * name = [self.ui_tableView cellForRowAtIndexPath:indexPath];
    if ([NSString checkNullString:name.ui_textField.text])
    {
        [PromptAction showInfoPrompt:@"昵称不能为空"];
        NSLog(@"%@",name.ui_textField.text);
        return;
    }
    for (int i=0; i<name.ui_textField.text.length; i++)
    {
        int a = [name.ui_textField.text characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff)
        {
            if (name.ui_textField.text.length==0 ||
                name.ui_textField.text.length>16)
            {
                [PromptAction showInfoPrompt:@"昵称超出范围"];
                NSLog(@"%@",name.ui_textField.text);
            }
            else
            {
                NSDictionary * dic = [[NSDictionary alloc]init];
                dic = @{@"nickName":[NSString stringWithFormat:@"%@",name.ui_textField.text]};
                [ServiceManager refreshUserInfo:dic success:^(NSDictionary *data)
                 {
                     [PromptAction showSuccessPrompt:@"修改昵称成功"];
                     [self.navigationController popViewControllerAnimated:YES];
                 } failure:^{
                     [PromptAction showSuccessPrompt:@"修改昵称失败"];
                 }];
            }
        }
        else
        {
            if (name.ui_textField.text.length==0 ||
                name.ui_textField.text.length>32)
            {
                [PromptAction showInfoPrompt:@"昵称超出范围"];
                NSLog(@"%@",name.ui_textField.text);
            }
            else
            {
                NSDictionary * dic = [[NSDictionary alloc]init];
                dic = @{@"nickName":[NSString stringWithFormat:@"%@",name.ui_textField.text]};
                [ServiceManager refreshUserInfo:dic success:^(NSDictionary *data)
                 {
                     [PromptAction showSuccessPrompt:@"修改昵称成功"];
                     [self.navigationController popViewControllerAnimated:YES];
                 } failure:^{
                     [PromptAction showSuccessPrompt:@"修改昵称失败"];
                 }];
            }
        }
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows=1;
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    cell=[self userName];
    return cell;
}

-(XHLabelTextFieldCell *)userName
{
    XHLabelTextFieldCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHLabelTextFieldCellIdentifier];
    
    cell.ui_titleLabel.text = @"用户昵称";
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.ui_textField.text = [NSString stringWithFormat:@"%@",self.nickName];
    cell.ui_textField.textColor = [ColorManager colorWithHexString:@"#000000" alpha:0.5];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=50;

    return height;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0.1;
    return height;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
